package com.orangelife.mobile.main.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.common.jump.CommonJumper;
import com.common.sp.JsonSp;
import com.common.sp.UserSp;
import com.curry.android.http.OrangeErrorHandler;
import com.curry.android.http.VolleyHttpUtil;
import com.curry.android.util.ImageDownloader;
import com.curry.android.util.JSONHelper;
import com.curry.android.util.StringUtil;
import com.curry.orangelife.mobile.R;
import com.orangelife.mobile.main.biz.JumpUtil;
import com.orangelife.mobile.main.biz.MainHttpBz;
import com.orangelife.mobile.util.ToastHelper;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2015/11/13.
 */
public class Servicefragment extends Fragment {

    View homePageView = null;
    private List<Map<String, Object>> data_list = null;
    @Bind(R.id.iv_image1)
    ImageView ivImage1;
    @Bind(R.id.iv_image2)
    ImageView ivImage2;
    @Bind(R.id.iv_image3)
    ImageView ivImage3;
    @Bind(R.id.iv_image4)
    ImageView ivImage4;
    @Bind(R.id.iv_image5)
    ImageView ivImage5;
    @Bind(R.id.iv_image6)
    ImageView ivImage6;

    @Bind(R.id.tv_txt1)
    TextView tvText1;
    @Bind(R.id.tv_txt2)
    TextView tvText2;
    @Bind(R.id.tv_txt3)
    TextView tvText3;
    @Bind(R.id.tv_txt4)
    TextView tvText4;
    @Bind(R.id.tv_txt5)
    TextView tvText5;
    @Bind(R.id.tv_txt6)
    TextView tvText6;

    @Bind(R.id.tv_txt1_remark)
    TextView tvText1Remark;
    @Bind(R.id.tv_txt2_remark)
    TextView tvText2Remark;


    @Bind(R.id.rl_1)
    RelativeLayout rl_1;
    @Bind(R.id.rl_2)
    RelativeLayout rl_2;

    @Bind(R.id.ll_3)
    LinearLayout ll_3;
    @Bind(R.id.ll_4)
    LinearLayout ll_4;
    @Bind(R.id.ll_5)
    LinearLayout ll_5;
    @Bind(R.id.ll_6)
    LinearLayout ll_6;

    private ImageDownloader image = ImageDownloader.getImageDownloader();
    List<Map<String, Object>> list;

    public Servicefragment() {
        super();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (homePageView == null) {
            homePageView = inflater.inflate(R.layout.fragment_service,
                    container, false);
            ButterKnife.bind(this, homePageView);
            findView();
            requestFeatures();
        }

        ViewGroup parent = (ViewGroup) homePageView.getParent();
        if (parent != null) {
            parent.removeView(homePageView);
        }
        return homePageView;
    }

    private void findView() {
        String json = JsonSp.getHouseKeeperMenu2Json();
        if (!TextUtils.isEmpty(json)) {
            list = (List<Map<String, Object>>) JSONHelper.jsonToMap(json).get("pageEntity");
            if (list != null) {
                if (list.size() > 0) {
                    for (Map<String, Object> map : list) {
                        setItemValue(Integer.parseInt(map.get("sort").toString()), map);
                    }
                }
            }
        }

    }


    /**
     * 获得分类列表
     */
    public void requestFeatures() {
        if (data_list != null && data_list.size() > 0) {
            return;
        }
        MainHttpBz.requestFeatures(new VolleyHttpUtil.ResponseCallBack() {
            @Override
            public void handleResponse(JSONObject response, int i) {
                data_list = MainHttpBz.handleFeatures(response);
                if (data_list != null && data_list.size() > 0) {
                    for (Map<String, Object> map : data_list) {
                        setItemValue((int) Float.parseFloat(String.valueOf(map.get("sort"))), map);
                    }
                    JsonSp.setHouseKeeperMenu2Json(response.toString());
                }
            }
        }, MainHttpBz.FETRUES_TYPE_2);
    }


    private void setItemValue(int sort, Map<String, Object> map) {
        switch (sort) {
            case 1:
                image.download(map.get("icon") + "", ivImage1);
                tvText1.setText(map.get("modeName") + "");
                tvText1Remark.setText(map.get("summary") + "");
                rl_1.setOnClickListener(new linstener(map));
                break;
            case 2:
                image.download(map.get("icon") + "", ivImage3);
                tvText3.setText(map.get("modeName") + "");
                ll_3.setOnClickListener(new linstener(map));
                break;
            case 3:
                image.download(map.get("icon") + "", ivImage5);
                tvText5.setText(map.get("modeName") + "");
                ll_5.setOnClickListener(new linstener(map));
                break;
            case 4:
                image.download(map.get("icon") + "", ivImage2);
                tvText2.setText(map.get("modeName") + "");
                tvText2Remark.setText(map.get("summary") + "");
                rl_2.setOnClickListener(new linstener(map));
                break;
            case 5:
                image.download(map.get("icon") + "", ivImage4);
                tvText4.setText(map.get("modeName") + "");
                ll_4.setOnClickListener(new linstener(map));
                break;
            case 6:
                image.download(map.get("icon") + "", ivImage6);
                tvText6.setText(map.get("modeName") + "");
                ll_6.setOnClickListener(new linstener(map));
                break;
            default:
                break;
        }
    }

    class linstener implements View.OnClickListener {
        Map<String, Object> map;

        public linstener(Map<String, Object> map) {
            this.map = map;
        }

        @Override
        public void onClick(View v) {
            Map<String,Object> trackmap=new HashMap<>();
            trackmap.put("UI_ELMT",String.valueOf(map.get("modeName")));


            if (map.get("isopen").equals("1")) {
                if (map.get("iscert").equals("1")) {//需要认证
                    if (!StringUtil.isContainBlank(UserSp.getLoginID())) {
                        JumpUtil.jumpActivity(getActivity(), String.valueOf(map.get("uri")),trackmap);
                    } else {
                        CommonJumper.jumpToLogin(getActivity());
                    }
                } else {
                    //跳转功能
                    JumpUtil.jumpActivity(getActivity(), String.valueOf(map.get("uri")),trackmap);
                }
            } else {
                ToastHelper.getInstance()._toast(map.get("openInfo") + "");
            }

        }
    }


}
